class Node {
    constructor(data) {
        this.element = data;
        this.next = null;
    }
}
export default class CircularLinkedList {
    /**
     * 头结点
     */
    #head = null;
    /**
     * 链表长度
     */
    #count = 0;

    /**
     * 向链表尾部添加元素
     * @param {any} data 
     * @returns {Node} node
     */
    push(data) {
        const node = new Node(data)

        if (this.#head === null) {
            this.#head = node;
            node.next = this.#head
        } else {
            const currentNode = this.getNodeByIndex(this.#count - 1)
            currentNode.next = node;
            node.next = this.#head
        }

        this.#count++
        return node
    }

    /**
     * 指定位置插入节点
     * @param {any} data 
     * @param {number} index 
     * @returns true | false
     */
    insert(data, index) {
        if (index >= 0 && index <= this.#count) {
            const node = new Node(data)
            if (index === 0) { // 如果插入的位置在链表头
                if (this.#head === null) {
                    this.#head = node;
                    node.next = this.#head;
                } else {
                    node.next = this.#head;
                    const lastNode = this.getNodeByIndex(this.#count - 1);
                    lastNode.next = node;
                    this.#head = node;
                }
            } else {
                const previoustNode = this.getNodeByIndex(index - 1);
                node.next = previoustNode.next;
                previoustNode.next = node;
            }
            this.#count++;
            return true;
        }

        // 未找到节点，报错
        console.error(`该链表没有对应索引为${index}的节点`)
        return false;
    }

    /**
     * 根据索引删除指定节点
     * @param {*} index 
     * @returns {Node} removed node
     */
    removeNodeByIndex(index) {
        let result = null
        if (index >= 0 && index < this.#count) {
            if (index === 0) {
                result = this.#head
                if (this.#count === 1) {

                    this.#head = null
                } else {
                    const tail = this.getNodeByIndex(this.#count - 1)
                    tail.next = this.#head.next
                    this.#head = this.#head.next
                }
            } else {
                const previoustNode = this.getNodeByIndex(index - 1)
                result = previoustNode.next
                previoustNode.next = previoustNode.next.next
            }
            this.#count--
        }

        return result
    }

    /**
     * 根据索引查找指定节点
     * @param {string | number} index 
     * @returns {Node} node
     */
    getNodeByIndex(index) {
        if (index < 0 && index >= this.#count) {
            console.error(`该链表没有对应索引为${index}的节点`);
            return
        };
        let result = this.#head;
        for (let i = 0; i < index; i++) {
            result = result.next;
        }
        return result
    }

    /**
     * 获取链表长度
     * @returns 链表长度
     */
     size() {
        return this.#count
    }

    
    /**
     * 是否为空链表
     * @returns true | false
     */
     isEmpty(){
        return this.#count === 0
    }

    /**
     * 打印链表
     */
     printList() {
        for (let i = 0; i < this.#count; i++) {
            console.log(this.getNodeByIndex(i));
        }
    }
}